﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SmartDevice.Connectivity; // Add Reference

namespace DeviceExplorer
{
    public partial class Form1 : Form
    {
        public static DatastoreManager dsmObject;
        public static Device ConnectedDevice;

        public Form1()
        {
            InitializeComponent();
            dsmObject = new DatastoreManager(1033);
            
 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            IEnumerable<Platform> platformcollection = dsmObject.GetPlatforms();

            foreach (Platform objplatform in platformcollection)
            {
                
              IEnumerable<Device> pdeviceCollection = objplatform.GetDevices();
                foreach (Device objdevice in pdeviceCollection)
                {
                    listBox1.Items.Add(objdevice.Name);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String str = listBox1.SelectedItem.ToString();
           IEnumerable<Platform> platformcollection = dsmObject.GetPlatforms();

            //Iterate through array of platforms 
            foreach (Platform objplatform in platformcollection)
            {
                IEnumerable<Device> pdeviceCollection = objplatform.GetDevices();
                foreach (Device objdevice in pdeviceCollection)
                {
                    if (objdevice.Name.Equals(str))
                    {
                        objdevice.Connect();
                        ConnectedDevice = objdevice;
                        return;
                    }
                }//foreach (Device objdevice in devices_list)
            }//foreach (Platform objplatform in platform_Array)

        }
    }
}
